package com.freesky.biz.report.service.impl;

import com.freesky.biz.report.domain.TReportDayGrant;
import com.freesky.biz.report.domain.TReportDayStock;
import com.freesky.biz.report.domain.TReportProject;
import com.freesky.biz.report.mapper.ReportDayStockMapper;
import com.freesky.biz.report.service.IReportDayStockService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReportDayStockService implements IReportDayStockService {

    @Autowired
    private ReportDayStockMapper reportDayStockMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;

    @Override
    public List<TReportDayStock> findList(TReportDayStock entity) {
        return reportDayStockMapper.findList(entity);
    }

    @Override
    public List<TReportDayGrant> selectGrantList(TReportDayGrant entity) {
        return reportDayStockMapper.selectGrantList(entity);
    }

    @Override
    public List<TReportDayGrant> selectGrantDayList(TReportDayGrant entity) {
        return reportDayStockMapper.selectGrantDayList(entity);
    }

    @Override
    public AjaxResult exportFile(TReportDayStock entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("REPORT_DAY_STOCK");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_FAIL.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TReportDayStock(), IReportDayStockService.class,
                "findList", "报表（按天）",Long.valueOf(exportRecord.getId()),sysUser.getUserName());
        return AjaxResult.success();
    }

    @Override
    public List<TReportDayStock> selectReportByDayList(TReportDayStock entity) {
        return reportDayStockMapper.selectReportByDayList(entity);
    }

    @Override
    public TReportProject selectReportDaySummary(TReportDayStock entity) {
        return reportDayStockMapper.selectReportDaySummary(entity);
    }

    @Override
    public List<TReportProject> selectReportProjectList(TReportProject entity) {
        return reportDayStockMapper.selectReportProjectList(entity);
    }

    @Override
    public TReportProject selectSummaryReportProject(TReportProject entity) {
        return reportDayStockMapper.selectSummaryReportProject(entity);
    }
}
